#define _CRT_SECURE_NO_WARNINGS

class Solution {
public:
    int _isBalancedR(TreeNode* root)
    {
        if (root == nullptr)
            return 0;
        int left = _isBalancedR(root->left);
        if (left == -1)
            return -1;
        int right = _isBalancedR(root->right);
        if (right == -1)
            return -1;
        int ret;
        if (abs(left - right) > 1)
            return -1;
        return ret = (left > right) ? left + 1 : right + 1;
    }
    bool isBalanced(TreeNode* root) {
        if (_isBalancedR(root) == -1)
            return false;
        return true;
    }
};

class Solution {
public:
    void _GetTreePathR(TreeNode* root, vector<string>& vs, string s)
    {
        if (root && root->left == nullptr && root->right == nullptr)
        {
            s += (to_string(root->val));
            vs.push_back(s);
            return;
        }
        if (root == nullptr)
            return;
        s += (to_string(root->val) + "->");
        _GetTreePathR(root->left, vs, s);
        _GetTreePathR(root->right, vs, s);
    }
    vector<string> binaryTreePaths(TreeNode* root) {
        vector<string> vs;
        string s;
        _GetTreePathR(root, vs, s);
        return vs;
    }
};

class Solution {
public:
    void _GetTreePathR(TreeNode* root, vector<string>& vs, string& s)
    {
        if (root && root->left == nullptr && root->right == nullptr)
        {
            string tmp = to_string(root->val);
            s += (tmp);
            vs.push_back(s);
            s.erase(s.end() - tmp.size(), s.end());
            return;
        }
        if (root == nullptr)
            return;
        string tmp = to_string(root->val) + "->";
        s += (tmp);
        _GetTreePathR(root->left, vs, s);
        _GetTreePathR(root->right, vs, s);
        s.erase(s.end() - tmp.size(), s.end());
    }
    vector<string> binaryTreePaths(TreeNode* root) {
        vector<string> vs;
        string s;
        _GetTreePathR(root, vs, s);
        return vs;
    }
};

class Solution {
public:
    void _GetSumR(TreeNode* root, int& sum)
    {
        if (root && root->left && !(root->left->left) && !(root->left->right))
            sum += root->left->val;
        if (root == nullptr)
            return;
        _GetSumR(root->left, sum);
        _GetSumR(root->right, sum);
    }
    int sumOfLeftLeaves(TreeNode* root) {
        int sum = 0;
        _GetSumR(root, sum);
        return sum;
    }
};